package com.wgchao.identityrightsmanagement.application.controller;

import com.wgchao.identityrightsmanagement.application.service.PermissionApplicationService;
import com.wgchao.identityrightsmanagement.application.service.PermissionInfo;
import com.wgchao.identityrightsmanagement.infrastructure.utils.ResponseData;
import com.wgchao.identityrightsmanagement.query.PermissionQuery;
import com.wgchao.identityrightsmanagement.query.vo.AccountPermissionVo;
import com.wgchao.identityrightsmanagement.query.vo.PermissionDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: PermissionController
 * @author: wgchao
 * @createTime: 2020/6/28 4:41 PM
 * @Description: 授权管理
 */
@RestController
@RequestMapping("/sntk/sys/permission")
@Api(value = "PermissionController", description = "授权管理")
public class PermissionController {
    @Autowired
    private PermissionApplicationService permissionApplicationService;
    @Autowired
    private PermissionQuery permissionQuery;

    @PostMapping("/office/{departmentCode}/{officeCode}/{proMark}")
    @ApiOperation(value = "为部门职位授权")
    public ResponseData assignPermissionsToOffice(@ApiParam(name = "departmentCode", value = "部门编号", required = true) @PathVariable String departmentCode,
                                                  @ApiParam(name = "officeCode", value = "职位编号", required = true) @PathVariable String officeCode,
                                                  @ApiParam(name = "proMark", value = "项目编号", required = true) @PathVariable String proMark,
                                                  @ApiParam(name = "permissionInfos", value = "授权信息", required = true) @RequestBody List<PermissionInfo> permissionInfos){
        permissionApplicationService.assignPermissionsToOffice(departmentCode, officeCode, proMark, permissionInfos, "admin");
        return ResponseData.success();
    }


    @GetMapping("/office/{departmentCode}/{officeCode}/{proMark}")
    @ApiOperation(value = "查询部门角色在项目下的授权")
    public ResponseData<PermissionDetailVo> searchAllPermissionsOfOffice(@ApiParam(name = "departmentCode", value = "部门编号", required = true) @PathVariable String departmentCode,
                                                                               @ApiParam(name = "officeCode", value = "职位编号", required = true) @PathVariable String officeCode,
                                                                               @ApiParam(name = "proMark", value = "项目编号", required = true) @PathVariable String proMark){
        PermissionDetailVo permissionInfos = permissionQuery.searchAllPermissionsOfOffice(officeCode, departmentCode, proMark);
        return ResponseData.success().setData(permissionInfos);
    }


    @GetMapping("/office/{username}/{proMark}")
    @ApiOperation(value = "查询账户在项目下的授权")
    public ResponseData<AccountPermissionVo> searchAllPermissionsOfAccount(@ApiParam(name = "username", value = "账户名", required = true) @PathVariable String username,
                                                                           @ApiParam(name = "proMark", value = "项目编号", required = true) @PathVariable String proMark){
        AccountPermissionVo permissionInfos = permissionQuery.searchAllPermissionsOfAccount(username, proMark);
        return ResponseData.success().setData(permissionInfos);
    }

}
